Spring MVC Login Form Example Tutorials 您所在的位置:网站首页 servlet java Spring MVC Login Form Example Tutorials

Spring MVC Login Form Example Tutorials

2023-04-10 12:16| 来源: 网络整理| 查看: 265

Spring MVC Login Form is a basic example for all spring based applications. We can hardly imagine any web application without forms in it because forms have their own importance in web application development.

In this tutorial, we are going to see how forms are used in Spring Framework, and how spring forms are different from normal HTML forms.

Prerequisites for Spring MVC Login Form:

As part of this tutorial, I am going to implement a simple Spring-based Login Form. Though it is a simple login form, it requires more understanding about each and every newer thing init.

Spring offers different types of annotations to handling forms. Here are the typical annotations that every spring developer should know.

@Controller : @Controller is an annotation, used in Spring MVC Framework. @Controller annotation is a sub annotation of @Cmponent annotation. We can use @Controller annotation on top of the class only. It indicates that a particular class serves the role of controller in the MVC pattern. @Controller annotation acts as a stereotype for the annotated class, indicating its roles. @RequestMapping : Like @Controller annotation, @RequestMapping annotation is also used in Spring MVC Framework. @RequestMapping annotation can be applied on top of the class and/or methods, and it should be used along with the @Controller annotation. It is used to bind an HTTP request to spring components or handler methods. Class level mapping binds a specific request to the Spring component (class), whereas method level mapping binds a specific request to a specific handler method. @ModelAttribute : @ModelAttribute annotation can be used on top of a method or method argument. An @ModelAttribute on a method is used to populate the model with commonly needed attributes for example to fill a drop-down with states. @ModelAttribute on method arguments refers to a specific property in Model class (MVC). Used to bind the data from form to controller It should be used along with @RequestMapping in Controllers @ModelAttribute is invoked before the actual controller method starts execution along with @RequestMapping

Recommended: Spring with Hibernate Integration Complete Example

Example for Spring MVC Login Form:

Here we are going to implement the Spring MVC Login form step by step.

Download Example : Spring MVC Login Form Example with STS (NEW)

Project Structure:

Follow below project structure to add described files below.

Spring MVC Login Form

Login.jsp Spring Login Form User Name Password ${error} Points To Note : On the above login form, we used tag. Which is given by the spring framework. To use this tag, we need to include the below taglib directory on top of the jsp page. The main advantage of using the is,  spring automatically binds the form data to the model bean whenever we submit this form. To make it work, the properties in the model class should be equal to the name of form data elements. For instance, we have created a text box field with the name userName and password field with the name password. The same names should be used as part of the Model class attributes.

Create a Login Model :

LoginBean.java package com.spring.controller; public class LoginBean { private String userName; private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }

Recommended: Spring Boot MVC Login Form Example

servlet-context.xml

Points To Note :

 , informs Spring container that we are going to use annotations in this application and can be used to identify annotations and to perform respective functions. Created a ViewResolver bean and mapped with the folder name where our actual views (JSP) are present as prefix and extension as a suffix Component scanning with , is telling spring that it should search the classpath for all the classes under com.spring.controller and look at each class to see if it has a @Controller, or @Repository, or @Service, or @Component and if it does then Spring will register the class with the bean factory as if you had typed in the XML configuration files.

Configure the web.xml File :

web.xml contextConfigLocation /WEB-INF/spring/root-context.xml org.springframework.web.context.ContextLoaderListener appServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/spring/appServlet/servlet-context.xml 1 appServlet /

Points To Note :

Define DispatcherServlet, which will act as a front controller to route the requests

Create a Spring Controller :

LoginController.java package com.spring.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class LoginController { @RequestMapping(value = "/login", method = RequestMethod.GET) public String init(Model model) { model.addAttribute("msg", "Please Enter Your Login Details"); return "login"; } @RequestMapping(method = RequestMethod.POST) public String submit(Model model, @ModelAttribute("loginBean") LoginBean loginBean) { if (loginBean != null && loginBean.getUserName() != null & loginBean.getPassword() != null) { if (loginBean.getUserName().equals("chandra") && loginBean.getPassword().equals("chandra123")) { model.addAttribute("msg", loginBean.getUserName()); return "success"; } else { model.addAttribute("error", "Invalid Details"); return "login"; } } else { model.addAttribute("error", "Please enter Details"); return "login"; } } }

Points To Note :

We have created two handler methods

init This is a default handler method for HTTP GET requests. The RequestMapping url is “/login”. submit This method is called whenever the form get submitted because the HTTP type for this method is POST. All the form elements are fed into the LoginBean object and we retrieve them using @ModelAttribute annotation Run It :

To run this application, we can make use of the below URL.

http://localhost:8080/Spring-MVC-LoginForm/login

Spring MVC Login Form Valid CredentialsError Response :

Spring Login Form Invalid CredentialsGiving Valid credentials :

Spring MVC Login Form Valid CredentialsSuccess Page :

Spring MVC Login Success Message

The complete Example is available for download : Spring MVC Login Form Example with STS (NEW)

Happy Learning 🙂

Download Example zip Spring MVC Login Form Example with STS (NEW) File size: 28 KB Downloads: 23705 zip Spring MVC Login Form Example With Netbeans File size: 18 MB Downloads: 33601 Related Posts Spring Boot MVC Example Tutorials Spring MVC Form Validation Example Spring Boot Validation Login Form Example Spring Web MVC Framework Flow Python Selenium Automate the Login Form Spring MVC HelloWorld Spring Form Custom Validation Example Spring MVC Tiles Example (Apache Tiles) Python – Flask Login Form Example Spring Hibernate Example Basic Android Login Form Example AngularJs Directive Example Tutorials How to Get All Spring Beans Details Loaded in ICO @Component,@Service,@Repository,@Controller in spring Spring Boot FileUpload Ajax Example


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有